/*
提交链接:https://leetcode.cn/problems/max-area-of-island/description/
695. 岛屿的最大面积
赖德檀 2024/10/7
*/

class Solution {
public:
int ans=0;
void dfs(vector<vector<int>>& grid,int i,int j)
{
    int n=grid.size();
    int m=grid[0].size();
    grid[i][j]=0;
    ans++;
    if(i-1>=0&&grid[i-1][j]==1) dfs(grid,i-1,j);
    if(i+1<n &&grid[i+1][j]==1) dfs(grid,i+1,j);
    if(j-1>=0&&grid[i][j-1]==1) dfs(grid,i,j-1);
    if(j+1<m &&grid[i][j+1]==1) dfs(grid,i,j+1);
}


    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int sum = 0;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[i].size();j++)
            {
                if(grid[i][j]==1)
                {
                dfs(grid,i,j);
                sum=max(ans,sum); 
                ans=0;                   
                }

            }
        }
        return sum;
    }
};